extern crate serde_json;

use self::serde_json::{json, Value};
use super::{cur_time, do_cache, do_cache_ret, do_node, do_node_ret};
use base::base::{VBase, VMap};
use std::{thread, time};

#[allow(dead_code)]
pub async fn ck_rule(p: String, val: Value) {
    let ten_millis = time::Duration::from_millis(30);
    thread::sleep(ten_millis);

    let cmd = vec![
        "all", "code1", "code2", "tcode", "task", "task2", "task3", "psn", "run", "psn2", "psn3",
    ];

    let val = val.kobj().unwrap();

    for d in cmd.iter() {
        //println!("===> {d}");

        let m = rule_cmd(d);

        match val.k_val(d) {
            Some(v) => {
                do_node_ret(p.clone(), m.clone(), json!(v)).await;
                do_cache_ret(p.clone(), m, json!(v)).await;
            }
            None => {
                do_node(p.clone(), m.clone()).await;
                do_cache(p.clone(), m).await;
            }
        }
    }
}

#[allow(dead_code)]
pub fn rule_cmd(cmd: &str) -> Value {
    match cmd {
        "all" => json!({"line": "1", "tp": "code", "sub": "all"}),
        "run" => json!({"line": "1", "tp": "task", "sub": "run"}),
        "code1" | "code2" => json!({"line": "1", "tp": "code", "sn": cmd}),
        "tcode" => json!({"line": "1", "tp": "tech", "sn": "te1"}),
        "task" => json!({"line": "1", "tp": "task", "sn": "task1"}),
        "task2" | "task3" => json!({"line": "1", "tp": "task", "sn": cmd}),
        "psn" => json!({"line": "1", "tp": "task", "sub": "psn", "sn": "task1"}),
        "psn2" => json!({"line": "1", "tp": "task", "sub": "psn", "sn": "task2"}),
        "psn3" => json!({"line": "1", "tp": "task", "sub": "psn", "sn": "task3"}),
        _ => panic!("rule_cmd"),
    }
}

#[allow(dead_code)]
pub fn rule_data() -> Value {
    let pstart = cur_time("%Y-%m-%d 00:00:00");
    let c = cur_time("%y%m%d");

    json!({
        "code1": {
            "sn": "code1",
            "name": "name_code1",
            "v": [
            {"type": "fix", "name": "fix", "val": "AAA", "num": 3},
            {"type": "date", "name": "gb34014", "date": "gb34014"},
            {"type": "date", "name": "gb16735", "date": "gb16735"},
            {"type": "date", "name": "YYM", "date": "YYM"},
            {"type": "num", "name": "num", "range": "day", "val": 1, "max": 5, "num": 3},
            {"type": "gnum1", "name": "gnum1", "range": "day", "val": 1, "max": 5, "num": 3},
            {"type": "num32", "name": "num32", "range": "day", "val": 1, "max": 5, "num": 3},
            ]
        },
        "code1_1": {
            "sn": "code1",
            "name": "name_code1",
            "cnum": 1,
            "cur": c,
            "v": [
            {"type": "fix", "name": "fix", "keyv": "val", "num": 3},
            {"type": "date", "name": "gb34014", "date": "gb34014"},
            {"type": "date", "name": "gb16735", "date": "gb16735"},
            {"type": "date", "name": "YYM", "date": "YYM"},
            {"type": "num", "name": "num", "range": "day", "val": 1, "max": 5, "num": 3},
            {"type": "gnum1", "name": "gnum1", "range": "day", "val": 1, "max": 5, "num": 3},
            {"type": "num32", "name": "num32", "range": "day", "val": 1, "max": 5, "num": 3},
            ]
        },
        "code1_3": {
            "sn": "code1",
            "name": "name_code1",
            "cnum": 2,
            "cur": c,
            "v": [
            {"type": "fix", "name": "fix", "val": "AAA", "num": 3},
            {"type": "date", "name": "gb34014", "date": "gb34014"},
            {"type": "date", "name": "gb16735", "date": "gb16735"},
            {"type": "date", "name": "YYM", "date": "YYM"},
            {"type": "num", "name": "num", "range": "day", "val": 1, "max": 5, "num": 3},
            {"type": "gnum1", "name": "gnum1", "range": "day", "val": 1, "max": 5, "num": 3},
            {"type": "num32", "name": "num32", "range": "day", "val": 1, "max": 5, "num": 3},
            ]
        },
        "code2": {
            "sn": "code2",
            "name": "name_code2",
            "v": [
            {"type": "fix", "name": "fix", "val": "AAA", "num": 3},
            {"type": "date", "name": "gb34014", "date": "gb34014"},
            {"type": "date", "name": "gb16735", "date": "gb16735"},
            {"type": "date", "name": "YYM", "date": "YYM"},
            {"type": "num", "name": "num", "range": "day", "val": 1, "max": 5, "num": 3},
            {"type": "gnum1", "name": "gnum1", "range": "day", "val": 1, "max": 5, "num": 3},
            {"type": "num32", "name": "num32", "range": "day", "val": 1, "max": 5, "num": 3},
            ]
        },
        "code2_3": {
            "sn": "code2",
            "name": "name_code2",
            "cnum": 1,
            "cur": c,
            "v": [
            {"type": "fix", "name": "fix", "val": "AAA", "num": 3},
            {"type": "date", "name": "gb34014", "date": "gb34014"},
            {"type": "date", "name": "gb16735", "date": "gb16735"},
            {"type": "date", "name": "YYM", "date": "YYM"},
            {"type": "num", "name": "num", "range": "day", "val": 1, "max": 5, "num": 3},
            {"type": "gnum1", "name": "gnum1", "range": "day", "val": 1, "max": 5, "num": 3},
            {"type": "num32", "name": "num32", "range": "day", "val": 1, "max": 5, "num": 3},
            ]
        },
        "code2_4": {
            "sn": "code2",
            "name": "name_code2",
            "cnum": 2,
            "cur": c,
            "v": [
            {"type": "fix", "name": "fix", "val": "AAA", "num": 3},
            {"type": "date", "name": "gb34014", "date": "gb34014"},
            {"type": "date", "name": "gb16735", "date": "gb16735"},
            {"type": "date", "name": "YYM", "date": "YYM"},
            {"type": "num", "name": "num", "range": "day", "val": 1, "max": 5, "num": 3},
            {"type": "gnum1", "name": "gnum1", "range": "day", "val": 1, "max": 5, "num": 3},
            {"type": "num32", "name": "num32", "range": "day", "val": 1, "max": 5, "num": 3},
            ]
        },
        "code2_5": {
            "sn": "code2",
            "name": "name_code2",
            "cnum": 3,
            "cur": c,
            "v": [
            {"type": "fix", "name": "fix", "val": "AAA", "num": 3},
            {"type": "date", "name": "gb34014", "date": "gb34014"},
            {"type": "date", "name": "gb16735", "date": "gb16735"},
            {"type": "date", "name": "YYM", "date": "YYM"},
            {"type": "num", "name": "num", "range": "day", "val": 1, "max": 5, "num": 3},
            {"type": "gnum1", "name": "gnum1", "range": "day", "val": 1, "max": 5, "num": 3},
            {"type": "num32", "name": "num32", "range": "day", "val": 1, "max": 5, "num": 3},
            ]
        },
        "run": "2024-01-01 01:01:01#task1",
        "task": {
            "sn": "task1",
            "pstart": "2024-01-01 01:01:01",
            "pend": "2024-02-01 01:01:01",
            "tech": "te1",
            "pnum": 3,
            "state": "open",
            "code": "TTTT",
        },
        "run2": "2024-01-01 02:01:01#task2",
        "task2": {
            "sn": "task2",
            "pstart": "2024-01-01 02:01:01",
            "pend": "2024-02-01 01:01:01",
            "tech": "te1",
            "pnum": 1,
            "state": "open",
            "code": "TTTT",
        },
        "run3": "2024-01-01 03:01:01#task3",
        "task3": {
            "sn": "task3",
            "pstart": "2024-01-01 03:01:01",
            "pend": "2024-02-01 01:01:01",
            "tech": "te1",
            "pnum": 1,
            "state": "open",
            "code": "TTTT",
        },
        "tcode": {
            "sn": "te1",
            "name": "name_te1",
            "pstart": pstart,
            "code": {
                "M010": {
                    "rule": "code1",
                    "ext": [
                    {"sn": "c1", "rule": "code1"},
                    {"sn": "c2", "rule": "code1"},
                    ]
                },
                "M020": {
                    "rule": "code2",
                    "ext": [
                    {"sn": "c1", "rule": "code2"},
                    {"sn": "c2", "rule": "code2"},
                    ]
                }
            }
        }
    })
}
